<html>
<head><meta charset="utf-8"><title>will specialization allow From of generics? · wg-traits · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/index.html">wg-traits</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/will.20specialization.20allow.20From.20of.20generics.3F.html">will specialization allow From of generics?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="158068050"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/will%20specialization%20allow%20From%20of%20generics%3F/near/158068050" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/will.20specialization.20allow.20From.20of.20generics.3F.html#158068050">(Feb 11 2019 at 21:42)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="k">struct</span> <span class="nc">Wrapper</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="n">T</span><span class="p">);</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">From</span><span class="o">&lt;</span><span class="n">Wrapper</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">from</span><span class="p">(</span><span class="n">other</span>: <span class="nc">Wrapper</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">other</span><span class="p">.</span><span class="mi">0</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>This fails in today's Rust because of the standard library's <code>impl From&lt;T&gt; for T</code>. Would the current proposed design of specialization allow this somehow?</p>



<a name="158080105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/will%20specialization%20allow%20From%20of%20generics%3F/near/158080105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/will.20specialization.20allow.20From.20of.20generics.3F.html#158080105">(Feb 12 2019 at 00:37)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> I don't believe so, because this is a violation of the orphan rules<br>
which state that if a type parameter appears in the <code>Self</code> type of an impl for a non-local trait, it must be "covered" by a local type</p>



<a name="158080161"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/will%20specialization%20allow%20From%20of%20generics%3F/near/158080161" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/will.20specialization.20allow.20From.20of.20generics.3F.html#158080161">(Feb 12 2019 at 00:38)</a>:</h4>
<p>e.g. the following example does compile:</p>
<div class="codehilite"><pre><span></span><span class="k">struct</span> <span class="nc">Wrapper</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="n">T</span><span class="p">);</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">OtherWrapper</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="n">T</span><span class="p">);</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">From</span><span class="o">&lt;</span><span class="n">Wrapper</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">OtherWrapper</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">from</span><span class="p">(</span><span class="n">other</span>: <span class="nc">Wrapper</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">OtherWrapper</span><span class="p">(</span><span class="n">other</span><span class="p">.</span><span class="mi">0</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="158080180"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/will%20specialization%20allow%20From%20of%20generics%3F/near/158080180" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/will.20specialization.20allow.20From.20of.20generics.3F.html#158080180">(Feb 12 2019 at 00:38)</a>:</h4>
<p>due to <code>OtherWrapper</code> covering the type parameter <code>T</code></p>



<a name="158080189"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/will%20specialization%20allow%20From%20of%20generics%3F/near/158080189" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/will.20specialization.20allow.20From.20of.20generics.3F.html#158080189">(Feb 12 2019 at 00:38)</a>:</h4>
<p>the accepted specialization RFC does not propose changes to this rule</p>



<a name="158080207"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/will%20specialization%20allow%20From%20of%20generics%3F/near/158080207" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/will.20specialization.20allow.20From.20of.20generics.3F.html#158080207">(Feb 12 2019 at 00:39)</a>:</h4>
<p>(see <a href="http://smallcultfollowing.com/babysteps/blog/2015/01/14/little-orphan-impls/" target="_blank" title="http://smallcultfollowing.com/babysteps/blog/2015/01/14/little-orphan-impls/">this blog post</a> for more on the orphan rules)</p>



<a name="158080301"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/will%20specialization%20allow%20From%20of%20generics%3F/near/158080301" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/will.20specialization.20allow.20From.20of.20generics.3F.html#158080301">(Feb 12 2019 at 00:41)</a>:</h4>
<p>I know there is other work going on outside specialization to change the orphan rules to make them more permissive, though</p>



<a name="158080317"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/will%20specialization%20allow%20From%20of%20generics%3F/near/158080317" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tmandry <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/will.20specialization.20allow.20From.20of.20generics.3F.html#158080317">(Feb 12 2019 at 00:41)</a>:</h4>
<p>or at least interest. not sure what state all that is in</p>



<a name="158085432"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/will%20specialization%20allow%20From%20of%20generics%3F/near/158085432" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/will.20specialization.20allow.20From.20of.20generics.3F.html#158085432">(Feb 12 2019 at 02:34)</a>:</h4>
<p><a href="https://rust-lang.github.io/rfcs/2451-re-rebalancing-coherence.html" target="_blank" title="https://rust-lang.github.io/rfcs/2451-re-rebalancing-coherence.html"><code>feature(re_rebalance_coherence)</code></a> is live, but does not address this specific aspect, (due to the generic, I believe).</p>



<a name="158085495"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/will%20specialization%20allow%20From%20of%20generics%3F/near/158085495" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/will.20specialization.20allow.20From.20of.20generics.3F.html#158085495">(Feb 12 2019 at 02:34)</a>:</h4>
<p>That's why I'm curious if specialization (perhaps with rebalancing) would address it</p>



<a name="158085525"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/will%20specialization%20allow%20From%20of%20generics%3F/near/158085525" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/will.20specialization.20allow.20From.20of.20generics.3F.html#158085525">(Feb 12 2019 at 02:35)</a>:</h4>
<p>Since <em>conceptually</em>, <code>impl From&lt;T&gt; for T</code> could be seen as a specialization.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>